package view;
/**
 *
 * @author zhaoyingxi
 */
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JLabel;
import Control.AirPortInfo;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.ParseException;
import javax.swing.*;

import model.AirLine;
import Control.AirLineControl;


public class BookView extends JFrame {
    private Vector content;
    private Integer index;
    Book_Model model;
    public BookView(Vector content, Integer index,Book_Model model) {
        this.content = content;
        this.index = index;
        this.model = model;
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        bookNum.setBounds(new Rectangle(205, 43, 102, 20));
        jLabel1.setToolTipText("");
        jLabel1.setText("TAX��");
        jLabel1.setBounds(new Rectangle(84, 106, 101, 15));
        jLabel2.setBounds(new Rectangle(205, 106, 102, 15));
        jLabel3.setText("jLabel3");
        jLabel3.setBounds(new Rectangle(203, 183, 102, 15));
        jLabel4.setText("GST��");
        jLabel4.setBounds(new Rectangle(84, 164, 101, 15));
        jLabel5.setBounds(new Rectangle(205, 164, 102, 15));
        jButton1.setBounds(new Rectangle(100, 251, 81, 23));
        jButton1.setToolTipText("");
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        jButton1.setText("Submit");
        jButton1.addActionListener(new BookView_jButton1_actionAdapter(this));
        jButton2.setBounds(new Rectangle(217, 251, 81, 23));
        jButton2.setText("Cancel");
        jButton2.addActionListener(new BookView_jButton2_actionAdapter(this));
        resultLabel.setHorizontalAlignment(SwingConstants.CENTER);
        resultLabel.setHorizontalTextPosition(SwingConstants.CENTER);
        this.getContentPane().add(label);
        resultLabel.setBounds(new Rectangle(10, 206, 386, 15));
        this.getContentPane().add(bookNum);
        this.getContentPane().add(jLabel1);
        this.getContentPane().add(jLabel2);
        this.getContentPane().add(jLabel4);
        this.getContentPane().add(jLabel5);
        this.getContentPane().add(resultLabel);
        this.getContentPane().add(jButton2);
        this.getContentPane().add(jButton1);
        jLabel2.setText(AirPortInfo.getTAX());
                this.setResizable(false);
        jLabel5.setText(AirPortInfo.getGST());
        this.setBounds(400,300,450,450);
        this.setVisible(true);
        label.setText("Input Num��");
        label.setBounds(new Rectangle(84, 48, 101, 15));
    }
    JTextField bookNum = new JTextField();
    JLabel label = new JLabel();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JLabel jLabel4 = new JLabel();
    JLabel jLabel5 = new JLabel();
    JLabel resultLabel = new JLabel();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    public void jButton1_actionPerformed(ActionEvent e)  {
        String price = (String) ((Vector) content.get(this.index)).get(3);
        String discountPrice = (String) ((Vector) content.get(this.index)).get(
                4);
        String discountNum = (String) ((Vector) content.get(this.index)).get(6);
        String num = bookNum.getText();
        double money = 0;
        Double singlePrice = 0.0;
        if (Integer.parseInt(num) >= Integer.parseInt(discountNum)) {
            singlePrice = Double.parseDouble(discountPrice);
            NumberFormat percentFormat = NumberFormat.getPercentInstance(Locale.
                    CHINESE);
            Number tax = null;
            try {
                tax = percentFormat.parse(AirPortInfo.getTAX());
            } catch (ParseException ex) {
            }
            Number GST = null;
            try {
                GST = percentFormat.parse(AirPortInfo.getGST());
            } catch (ParseException ex1) {
            }
            singlePrice += singlePrice * tax.doubleValue();
            singlePrice += singlePrice * GST.doubleValue();
            money = Integer.parseInt(num) * singlePrice;
        } else {
            singlePrice = Double.parseDouble(price);
            NumberFormat percentFormat = NumberFormat.getPercentInstance(Locale.
                    CHINESE);
            Number tax = null;
            try {
                tax = percentFormat.parse(AirPortInfo.getTAX());
            } catch (ParseException ex2) {
            }
            Number GST = null;
            try {
                GST = percentFormat.parse(AirPortInfo.getGST());
            } catch (ParseException ex3) {
            }
            singlePrice += singlePrice * tax.doubleValue();
            singlePrice += singlePrice * GST.doubleValue();
            money = Integer.parseInt(num) * singlePrice;
        }
        String airLinNum = (String) ((Vector) content.get(this.index)).get(0);
        String levelNum = (String) ((Vector) content.get(this.index)).get(7);
        String soldNum = (String) ((Vector) content.get(this.index)).get(7);
        Integer levenNum = Integer.parseInt(levelNum)-Integer.parseInt(num);
        ((Vector) content.get(this.index)).remove(7);
        ((Vector) content.get(this.index)).add(7, Integer.parseInt(levelNum)-Integer.parseInt(num)+"");
        model.fireTableCellUpdated(this.index, 7);
        this.resultLabel.setText("success   "+
                           " SumMoney:" + money);
        AirLine airLine = AirLineControl.findByNum(airLinNum);
        airLine.setSumMoney(airLine.getSumMoney()+money);
        airLine.setSoldNum(airLine.getSoldNum()+Integer.parseInt(num));
        AirLineControl.update2(airLine);
    }
    public void jButton2_actionPerformed(ActionEvent e) {
        this.hide();
    }
}


class BookView_jButton2_actionAdapter implements ActionListener {
    private BookView adaptee;
    BookView_jButton2_actionAdapter(BookView adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}


class BookView_jButton1_actionAdapter implements ActionListener {
    private BookView adaptee;
    BookView_jButton1_actionAdapter(BookView adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}
